package com.hzjh.service.videolib.widget

import android.content.Context
import android.util.AttributeSet
import android.view.MotionEvent
import android.view.View
import android.widget.SeekBar
import com.hzjh.base.style.view.dialog.ConfirmDialog
import com.hzjh.base.utils.other.CommUtils
import com.hzjh.service.videolib.R
import com.shuyu.gsyvideoplayer.utils.NetworkUtils
import com.shuyu.gsyvideoplayer.video.StandardGSYVideoPlayer

class SampleCoreVideo @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null
) : StandardGSYVideoPlayer(context, attrs){
    private var listener:SeekBarVisableListener?=null
    private var moveX = 0f //横纵方向上的偏移moveX

    override fun showWifiDialog() {
        //4g播放提示弹框自定义
        if (mContext == null) {
            return
        }
        if (!NetworkUtils.isAvailable(mContext)) {
            startPlayLogic()
            return
        }
        ConfirmDialog.Builder(mContext)
            .setTitle(CommUtils.getString(R.string.dialog_title_wifi))
            .setMessage(CommUtils.getString(R.string.dialog_message_wifi))
            .setCancelButton(CommUtils.getString(R.string.dialog_message_cancel)){dialog, _ ->
                dialog.dismiss()
            }.setSureButton(CommUtils.getString(R.string.dialog_message_ok)) { dialog, _ ->
                dialog.dismiss()
                startPlayLogic()
            }.create().show()
    }

    override fun getLayoutId(): Int {
        return R.layout.vepub_my_video_layout
    }

    override fun onTouch(v: View?, event: MotionEvent?): Boolean {
        moveX = event!!.x
        if(v?.id == com.shuyu.gsyvideoplayer.R.id.surface_container){
            if(startButton?.visibility == View.VISIBLE){
                when(event?.action){
                    MotionEvent.ACTION_UP ->{
                        //全屏幕点击支持暂停播放
                        startButton?.performClick()
                    }
                }
                return true
            }
        }
        return super.onTouch(v, event)
    }

    override fun touchSurfaceMove(deltaX: Float, deltaY: Float, y: Float) {
        mChangeVolume = false //不支持改变声音
        mBrightness = false //不支持改变亮度
        super.touchSurfaceMove(deltaX, deltaY, y)
    }


    //滑动进度条设置
    override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) {
        super.onProgressChanged(seekBar, progress, fromUser)
        //进度条滑动
//        if(mHadSeekTouch) {
//            val totalTimeDuration = duration
//            val totalTime = CommonUtil.stringForTime(totalTimeDuration)
//            showProgressDialog(
//                moveX - mDownX,
//                CommonUtil.stringForTime(progress * duration / 100),
//                mSeekTimePosition,
//                totalTime,
//                totalTimeDuration
//            )
//        }else{
//            dismissProgressDialog()
//        }
    }

    override fun hideAllWidget() {
        super.hideAllWidget()
        listener?.visibilityChange(false)
    }
    override fun changeUiToNormal() {
        super.changeUiToNormal()
        listener?.visibilityChange(false)
    }

    override fun changeUiToPreparingShow() {
        super.changeUiToPreparingShow()
        listener?.visibilityChange(true)
    }

    override fun changeUiToPlayingShow() {
        super.changeUiToPlayingShow()
        listener?.visibilityChange(true)
    }

    override fun changeUiToPauseShow() {
        super.changeUiToPauseShow()
        listener?.visibilityChange(true)
    }

    override fun changeUiToPlayingBufferingShow() {
        super.changeUiToPlayingBufferingShow()
        listener?.visibilityChange(true)
    }

    override fun changeUiToCompleteShow() {
        super.changeUiToCompleteShow()
        listener?.visibilityChange(true)
    }

    override fun changeUiToError() {
        super.changeUiToError()
        listener?.visibilityChange(false)
    }

    override fun changeUiToPrepareingClear() {
        super.changeUiToPrepareingClear()
        listener?.visibilityChange(false)
    }

    override fun changeUiToPlayingBufferingClear() {
        super.changeUiToPlayingBufferingClear()
        listener?.visibilityChange(false)
    }

    override fun changeUiToClear() {
        super.changeUiToClear()
        listener?.visibilityChange(false)
    }

    override fun changeUiToCompleteClear() {
        super.changeUiToCompleteClear()
        listener?.visibilityChange(false)
    }
    fun setSeekbarListener(listener:SeekBarVisableListener){
        this.listener=listener
    }
}